<?php
class ImAction extends Action{
	
	private $org_name 		= 'qeebu';
	private	$app_name 		= 'teamin';
	private $client_id		= 'YXA68hIhcD1QEeS5XplZlbqk-Q';
	private $client_secret 	= 'YXA6Aaxm21EV1FjV_qoqO56sEJ5MUSw';
	private $access_token	= '';
	private $right_count	= 0;
	
	public function exe(){

		//$easemob = new HXPlatform_WebRequest ();
		//获取token , Token 活得一次 7天内有效. 
		$access = $this->postRequest (
			'https://a1.easemob.com/' . $this->org_name . '/' . $this->app_name . '/token', 
			'grant_type=client_credentials&client_id=' . $this->client_id . '&client_secret=' . $this->client_secret 
		);
		$access = json_decode ( $access, true );
		$access_token = $access ['access_token']; //获取access_token
		echo $access_token;
		$uid = 'aaaaaa'; //用户名
		$pwd = 'aaaaaa'; //密码
	}
	
	public function reg_us(){
		Database($_SESSION['cid']);
		$cid=$_SESSION['cid'];
		$audience_list=D('Audience')->get_users();
		foreach($audience_list as $val){
			$data['audience_imUsername']=md5($val['audience_username'].$cid);
			$data['audience_imPassword']=md5(123456);
			$data['audience_id']		=$val['audience_id'];
			D('Audience')->upt_audience($data);
			$this->reg_user($data['audience_imUsername'],$data['audience_imPassword']);
		}
		dump($this->right_count);
	}
	
	public function reg_user($uid,$pwd){
		$this->access_token='YWMtLGQzvo_zEeSG5fO0qkbN8QAAAUvPBzX2G8JgQ4LngRXnK2RN7ATWP2AVIOg';
		
		//注册用户
		$acc_add = $this->postRequest ( 
			'https://a1.easemob.com/' . $this->org_name . '/' . $this->app_name . '/users', 
			'{"username":"' . $uid . '","password":"' . $pwd . '"}', 
			$this->access_token
		);
		dump($acc_add);
		if($acc_add)
			$this->right_count++;
	}
	
	
	//token 有效时间 7天
	public function get_token(){
		$url = 'https://a1.easemob.com/qeebu/teamin/token';
		$data='{
			"grant_type": "client_credentials",
			"client_id": "'.$this->client_id.'",
			"client_secret": "'.$this->client_secret.'"
		}';
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		$ret = curl_exec($ch);
		dump($ret);
		curl_close($ch);
	}
	//删除指定用户
	public function del_imUsername_by_uid(){
		$acc_delete_by_uid = $this->postRequest ( 
			'https://a1.easemob.com/' . $org_name . '/' . $app_name . '/users/' . $uid, 
			'', 
			$access_token, 
			'DELETE'
		);
		$acc_delete_by_uid = json_decode ( $acc_delete_by_uid, true );
		print_r ( $acc_delete_by_uid );	
	}
	//批量删除用户
	public function del_imUsernames(){
		$acc_delete = $easemob->postRequest ( 
			'https://a1.easemob.com/' . $org_name . '/' . $app_name . '/users?limit=300', 
			'', 
			$access_token ,
			'DELETE'
		);
		$acc_delete = json_decode ( $acc_delete, true );
		print_r ( $acc_delete );	
	}
	//加好友
	public function add_imUsernmae(){
		$fuid='bbbbbb'; //好友用户名
		//添加用户关系      这里参数{"username":"aaaaaa1","password":"aaaaaa1"} 为用户管理员账号密码 ,需要后台手动添加
		$acc_contact = $easemob->postRequest ( 'https://a1.easemob.com/' . $org_name . '/' . $app_name . '/users/' . $uid . '/contacts/users/' . $fuid, '{"username":"aaaaaa","password":"aaaaaa"}', $access_token);
		$acc_contact = json_decode ( $acc_contact, true );
		
		print_r ( $acc_contact );	
	}
	
	
	/**
	 * 发送URL请求
	 * @param  string $url     要发送到的地址
	 * @param  string $query   需要发送的参数。参数需要以a=1&b=2的形式传递。
	 * @return string
	 */
	private function postRequest($url, $post_string = '', $Authorization = '', $type = 'POST') {
		$useragent = 'easemob.com API PHP5 Client 1.1 (curl) ' . phpversion ();
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		if (strlen ( $post_string ) >= 3) {
			curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_string );
		}
		if ($Authorization) {
			$header = array ('Authorization: Bearer ' . $Authorization );
			curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
		}
		
		//curl_setopt($ch, CURLOPT_USERPWD, "username:password");
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
		curl_setopt ( $ch, CURLOPT_USERAGENT, $useragent );
		curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
		switch ($type) {
			case 'DELETE' :
				curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "DELETE" );
				break;
			default :
				break;
		}
		curl_setopt ( $ch, CURLOPT_TIMEOUT, 30 );
		$result = curl_exec ( $ch );
		curl_close ( $ch );
		return $result;
	}
	
}
?>